<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="../css/plugins/summernote/summernote.css" rel="stylesheet">
    <link href="../css/ics/common.css" rel="stylesheet">
    <title>检查报告</title>

    <style>

        .jqGrid_wrapper {
            padding: 10px 15px;
            border: none;;
        }

        .head-nav {
            display: inline-block;
            vertical-align: middle;
        }

        .transfer-button-div {
            position: absolute;
            top: 10px;
            right: 25px;
        }

        .search-div {
            padding: 0 15px;
            height: 45px;
        }

        .search-input {
            width: 363px;
            height: 31px;
            visibility: hidden;
        }

        #gbox_report-emr, #gview_report-emr, .ui-jqgrid-hdiv, .ui-jqgrid-hbox, .ui-jqgrid-htable, .ui-jqgrid-bdiv, .ui-jqgrid-btable {
            width: 100% !important;
            padding: 0 !important;
            margin: 0 !important;
        }

    </style>
</head>

<body>

<div class="row">
    <div class="col-sm-4 col-sm-offset-8">
        <div class="input-group" style="padding: 10px 15px;">
            <input type="text" class="form-control" id="search-key" placeholder="患者编号 | 姓名">
            <span class="input-group-btn">
                <button type="button" class="btn btn-primary" onclick="search();">搜索</button>
            </span>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-sm-12">
        <div id="report-list" class="jqGrid_wrapper" style="text-align: center;width: 100%;">
            <div id="report-emr-div">
                <table id="report-emr"></table>
            </div>
            <div id="norecords">没有查询记录！</div>
            <div class="pagination">
                <a class="first" id="first_btn" data-action="first"><i class="icon-double-angle-left"></i></a>
                <a class="previous" id="previous_btn" data-action="previous"><i class="icon-angle-left"></i></a>
                <input id="page_num" type="text" onkeydown="designatedPage(event)"/>
                <a class="next" id="next_btn" data-action="next"><i class="icon-angle-right"></i></a>
                <a class="last" id="last_btn" data-action="last"><i class="icon-double-angle-right"></i></a>
            </div>
        </div>
    </div>
</div>

<script>
    //声明变量
    //分页描述文字
    var pageStr = null;
    //当前页
    var currentPage = 1;
    //总页数
    var totalPage = 1;

    var pageSize = 10;

    var cacheData;

    var myEmrDivHtml = $("#report-emr-div");
    var pagination = $('.pagination');
    var norecords = $("#norecords");
    var searchKey = $("#search-key");
    loadData();
    pageInit();

    function search() {
        currentPage = 1;
        loadData();
    }

    //初始化页面
    function pageInit() {
        $('.pagination').jqPagination({
            max_page: totalPage, //设置最大页 默认为1
            current_page: currentPage, //设置当前页 默认为1
            page_string: '当前第{current_page}页,共{max_page}页',
            paged: function (page) {
                if (page != currentPage) {
                    currentPage = page;
                    loadData();
                }
            }
        });

        // Add responsive to jqGrid
        $("#report-list").bind('resize', function () {
            var width = $('#report-list').width();
            $("#report-emr").setGridWidth(width);
        });

    }

    function loadData() {
    	mandy_get_my_emr_with_count(searchKey.val(), currentPage, pageSize, function (data) {
            cacheData = data;
            currentPage = data.current_page;
            totalPage = data.total_page;
            if (totalPage == 0) {
                totalPage = 1;
            }
            pagination.jqPagination('option', 'current_page', currentPage);
            pagination.jqPagination('option', 'max_page', totalPage);

            var list = data.list == null ? [] : data.list;

            if (list.length == 0) {
                norecords.show();
            } else {
                norecords.hide();
            }

            var index = 1;

            myEmrDivHtml.empty();
            myEmrDivHtml.append("<table id='report-emr'></table>");
            $("#report-emr").jqGrid({
                data: list,
                datatype: "local",
                height: "auto",
                autowidth: true,
                rowNum: pageSize,
//                rownumbers:true,
                colNames: ['ID', 'patientId', '序号', '患者编号', '姓名', '性别', '年龄', '医保类型', /*'诊断',*/ '文档数量', "操作"],
                colModel: [
                    {name: 'id', index: 'id', width: 0, hidden: true},
                    {name: 'patient.id', index: 'patient.id', width: 0, hidden: true},
                    {
                        name: 'index',
                        index: 'index',
                        width: 50,
                        align: 'center',
                        sortable: false,
                        formatter: function (cellvalue, options, rowObject) {
                            var sortNum = index % list.length == 0 ? list.length : index % list.length;
                            index++;
                            return sortNum;
                        }
                    },
                    {name: 'number', index: 'number', width: 130, align: 'center'},
                    {
                        name: 'patient.name', index: 'patient.name', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            return getPrivatePatientName(cellvalue);
                        }
                    },
                    {
                        name: 'patient.sex', index: 'patient.sex', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue == "female") {
                                return "女";
                            }
                            else if (cellvalue == "male") {
                                return "男";
                            }
                            else {
                                return "未知";
                            }
                        }
                    },
                    {
                        name: 'patient.birthday', index: 'patient.birthday', width: 50, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (cellvalue != null) {
                                return calculateAge(cellvalue);
                            }
                        }
                    },
                    {
                        name: 'insurance_type', index: 'insurance_type', width: 60, align: 'center',
                        formatter: function (cellvalue, options, rowObject) {
                            if (1 == cellvalue) {
                                return "医保";
                            }
                            else if (2 == cellvalue) {
                                return "工伤";
                            }
                            else {
                                return "自费";
                            }
                        }
                    },
//                    {
//                        name: 'clinic_list', index: 'clinic_list', width: 90, align: 'center',
//                        formatter: function (cellvalue, options, rowObject) {
//                            if (cellvalue != null && cellvalue.length > 0) {
//                                debugAlert(cellvalue[0]);
//                                return "<span class='' data_my_data=" + cellvalue[0].id + ">" + cellvalue[0].diagnose.name + "</span>";
//                            }
//                            else {
//                                return "<span></span>";
//                            }
//                        }
//                    },
                    {name: 'count', index: 'count', width: 50, align: 'center'},
                    {
                        name: 'option',
                        index: 'option',
                        width: 80,
                        align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            var optionHtml = '<a style="text-decoration:none;color:#fff;padding:5px 10px;background-color: #42D0A7; border-radius: 14px;" onclick="addReport(' + rowObject.id + ')" >上传检查报告</a>';
                            return optionHtml;
                        }
                    }
                ],
                viewrecords: true,
                hidegrid: false,
                subGrid: true,  // (1)开启子表格支持
                subGridRowExpanded: function (subgrid_id, row_id) {
                    var subgrid_table_id;
                    subgrid_table_id = subgrid_id + "_t";   // (3)根据subgrid_id定义对应的子表格的table的id

                    var subgrid_pager_id;
                    subgrid_pager_id = subgrid_id + "_pgr"  // (4)根据subgrid_id定义对应的子表格的pager的id

                    // (5)动态添加子报表的table和pager
                    $("#" + subgrid_id).html(
                            '<div class="jqGrid_wrapper" style="text-align: center">\
                            <div id="prcps_table">\
                            <table id="' + subgrid_table_id + '"></table>\
                    </div>\
                    <div id="' + subgrid_pager_id + '">没有查询记录！</div>\
                    </div>');

//                    var emrId=$("#report-emr").jqGrid('getRowData',row_id).id;
                    var patientId = $("#report-emr").jqGrid('getRowData', row_id)['patient.id'];
                    var emrId = $("#report-emr").jqGrid('getRowData', row_id)['id'];
                    loadItems(emrId, patientId, subgrid_table_id, subgrid_pager_id);


                },
//                multiselect: true,
//                 multiboxonly:true,
//                 beforeSelectRow: beforeSelectRow
                onSelectRow: function (rowid, status) {
//                    var key = "evaluation_prcp_list" + rowid;
//                   if(map.get(key) == null){
//                       $("#report-emr").expandSubGridRow(rowid);
//                       map.put(key,true);
//                   } else {
//                       $("#report-emr").collapseSubGridRow(rowid);
//                       map.put(key,null);
//                   }

                    var key = "evaluation_prcp_list";
                    var oldRowId = map.get(key);

                    if (oldRowId == null) {
                        $("#report-emr").expandSubGridRow(rowid);
                        map.put(key, rowid);
                    } else {
                        if (rowid != oldRowId) {
                            $("#report-emr").expandSubGridRow(rowid);
                            map.put(key, rowid);
                        } else {
                            map.put(key, null);
                        }

                        $("#report-emr").collapseSubGridRow(oldRowId);
                    }

                }
            });
//            //隐藏全选按钮
//            $("#cb_emr").addClass("hide");

            //隐藏+号
            $("#report-emr").setGridParam().hideCol("subgrid").trigger("reloadGrid");
            $("#report-emr tr:even").css("background","#F6F6F6");
        });

    }

    function addReport(emrId) {
        var param = {};
        param.emr_id = emrId;
        setParamToUrl(param);
        loadTitle('/report/upload_report_title.html');
    }


    function loadItems(emrId, patientId, subgrid_table_id, subgrid_pager_id) {
        mandy_get_inspection_report_list(patientId, function (data) {
//            debugger;
            var list = data.inspection_report_list == null ? [] : data.inspection_report_list;

            if (list.length == 0) {
                $("#" + subgrid_pager_id).show();
            } else {
                $("#" + subgrid_pager_id).hide();
            }

            var subIndex = 1;

            $("#" + subgrid_table_id).jqGrid({
                data: list,
                datatype: "local",
                height: "auto",
                autowidth: true,
                shrinkToFit: true,
                rowNum: list.length,
//                rownumbers:true,
                colNames: ['序号', '类别', '检查时间', '操作'],
                colModel: [
                    {
                        name: 'index',
                        index: 'index',
                        width: 40,
                        align: 'center',
                        sortable: false,
                        formatter: function (cellvalue, options, rowObject) {
                            var sortNum = subIndex % list.length == 0 ? list.length : subIndex % list.length;
                            subIndex++;
                            return sortNum;
                        }
                    },
                    {
                        name: 'type',
                        index: 'type',
                        width: 100,
                        align: "center"
                    },
                    {
                        name: 'check_time',
                        index: 'check_time',
                        width: 100,
                        align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            return new Date(cellvalue).format("YYYY-MM-DD");
                        }
                    },
                    {
                        name: 'option',
                        index: 'option',
                        width: 80,
                        align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            var reportId = rowObject.id;
                            return "<a href='#' style='text-decoration:underline;color:#6d9dcd' " +
                                    "onclick='openReport(\"" + emrId + "\",\"" + reportId + "\")'>查看</a>&nbsp;&nbsp;";
                        }
                    }
                ],
                viewrecords: true,
                hidegrid: false
            });
        });
    }

    function designatedPage(e) {
        var ev = window.event || e;
        var pageNum = $("#page_num").val();

        if (ev.keyCode == 13) {
            if (!/^[0-9]*$/.test(pageNum) || pageNum > totalPage || pageNum < 1) {
                toastr.error("请输入正确的页数!");
                return;
            }
        }
    }

    function openReport(emrId, reportId) {
        var param = {};
        param.emr_id = emrId;
        param.report_id = reportId;
        setParamToUrl(param);
        loadTitle('/report/upload_report_detail_title.html');
    }
</script>


</body>

</html>
